package com.yanqu.road.server.cmd.activity.gold;

import com.yanqu.road.connect.ServerLinkControl;
import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.ActivityDisperseGoldPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityDisperseGoldProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.ActivityDisperseGoldMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_ACTIVITY_DISPERSE_GOLD_BORADCAST, desc = "跨服送财推送")
public class ActivityDisperseGoldBoradCastCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        ActivityDisperseGoldProto.CrossActivityDisperseGoldRespMsg respMsg = ActivityDisperseGoldProto.CrossActivityDisperseGoldRespMsg.parseFrom(packet.getBytes());

        ActivityDisperseGoldInfo goldInfo = ActivityDisperseGoldPb.parseToDisperseGoldInfo(respMsg.getGoldInfo());
        //保存
        ActivityDisperseGoldMgr.addActivityDisperseGoldInfo(goldInfo);

        if(ActivityDisperseGoldMgr.isDisperseGoldEnd(goldInfo)){
            return;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(goldInfo.getActivityId());
        if(activityInfo == null) {
            return;
        }

        ActivityDisperseGoldProto.CrossActivityDisperseGoldRespMsg.Builder respMsgBuilder = ActivityDisperseGoldProto.CrossActivityDisperseGoldRespMsg.newBuilder();
        //活动结束前30秒或者有效期前10s,不推送给其他玩家
        long nowTime = System.currentTimeMillis() / 1000;
        if(activityInfo.getEndShowTime() - 30 < nowTime || ActivityDisperseGoldMgr.isDisperseGoldOutSendTime(goldInfo, 10)){
            //只给玩家本人发送
            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(goldInfo.getUserId());
            if (onlinePlayer != null) {
                ActivityDisperseGoldProto.ActivityDisperseGoldClientRespMsg.Builder builder = ActivityDisperseGoldProto.ActivityDisperseGoldClientRespMsg.newBuilder();
                builder.setGoldInfo(ActivityDisperseGoldPb.parseToDisperseGoldInfoTemp(goldInfo));
                onlinePlayer.sendPacket(Protocol.U_ACTIVITY_DISPERSE_GOLD_SYNC, builder);
            }
            return;
        }
        respMsgBuilder.setActivityId(activityInfo.getActivityId());
        respMsgBuilder.setGoldInfo(ActivityDisperseGoldPb.parseToDisperseGoldInfoTemp(goldInfo));
        int groupId = respMsg.getGroupId();
        if(groupId > 0){
            //按分组时，直接区分下发
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerList()) {
                int gId = GameUnionActivityGroupMgr.getGroupId(respMsg.getActivityId(), player.getUserId());
                if(gId == groupId || (gId == UnionActivityGroupHelper.NO_GROUP_ID && groupId <= 1)){
                    ActivityDisperseGoldProto.ActivityDisperseGoldClientRespMsg.Builder builder = ActivityDisperseGoldProto.ActivityDisperseGoldClientRespMsg.newBuilder();
                    builder.setGoldInfo(respMsg.getGoldInfo());
                    YanQuMessage msg = YanQuMessageUtils.buildMessageByte(Protocol.U_ACTIVITY_DISPERSE_GOLD_SYNC, builder.build().toByteArray());
                    msg.setPlayerId(player.getUserId());
                    msg.setLen(packet.getLen());
                    GamePlayerMgr.sendPacket(player.getUserId(), msg);
                }
            }
        }else {
            // 客户端散财
            YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Protocol.A_ACTIVITY_DISPERSE_GOLD_TRANSFER, respMsgBuilder);
            ServerLinkControl.send(0, pbMessage);
        }
    }
}
